package org.edu.modules.growth.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.edu.modules.growth.config.HttpURLConnectionUtil;
import org.edu.modules.growth.entity.bo.*;
import org.edu.modules.growth.service.IVeBaseManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VeBaseManageServiceImpl implements IVeBaseManageService {

    @Value("${common.host}")
    private String dirHost ;

    @Autowired
    ObjectMapper mapper;


    @Override
    public BasicResponseBO<List<VeBaseTeacherBO>> getTeacherByGH(String GH) {
        String url = ""+"?GH="+GH ;
        BasicResponseBO<List> b = getUrl(url,List.class) ;
        BasicResponseBO<List<VeBaseTeacherBO>> responseBO = new BasicResponseBO<>() ;
        responseBO.setSuccess(b.getSuccess()).setMessage(b.getMessage())
                .setResult(b.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(b .getResult()),VeBaseTeacherBO.class)) ;
        return responseBO;
    }

    @Override
    public BasicResponseBO<List<VeBaseSemBO>>  semesterVo() {
        String url = "common/veCommon/querySemesterList" ;
        BasicResponseBO<List> b = getUrl(url,List.class) ;
        BasicResponseBO<List<VeBaseSemBO>> responseBO = new BasicResponseBO<>() ;
        responseBO.setSuccess(b.getSuccess()).setMessage(b.getMessage())
                .setResult(b.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(b .getResult()),VeBaseSemBO.class)) ;
        return responseBO;
    }

    @Override
    public BasicResponseBO<List<VeBaseFacultBO>> facultAll() {
        String url = "common/veCommon/queryFacultyList" ;
        BasicResponseBO<List> b = getUrl(url,List.class) ;
        BasicResponseBO<List<VeBaseFacultBO>> responseBO = new BasicResponseBO<>() ;
        responseBO.setSuccess(b.getSuccess()).setMessage(b.getMessage())
                .setResult(b.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(b .getResult()),VeBaseFacultBO.class)) ;
        return responseBO;
    }

    @Override
    public BasicResponseBO<List<VeBaseDeptBO>> deptAll() {
        String url = "common/veCommon/queryDepartmentList?id=09d5e1e7f9b049008eee645c783a1d67" ;
        BasicResponseBO<List> b = getUrl(url,List.class) ;
        BasicResponseBO<List<VeBaseDeptBO>> responseBO = new BasicResponseBO<>() ;
        responseBO.setSuccess(b.getSuccess()).setMessage(b.getMessage())
                .setResult(b.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(b .getResult()),VeBaseDeptBO.class)) ;
        return responseBO;
    }

    @Override
    public BasicResponseBO<List<VeBaseZyzBO>> zyzAll() {
        String url = "" ;
        BasicResponseBO<List> b = getUrl(url,List.class) ;
        BasicResponseBO<List<VeBaseZyzBO>> responseBO = new BasicResponseBO<>() ;
        responseBO.setSuccess(b.getSuccess()).setMessage(b.getMessage())
                .setResult(b.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(b .getResult()),VeBaseZyzBO.class)) ;
        return responseBO;
    }

    /**
     * 调用公告数据接口
     */
    private <T> BasicResponseBO<T> getUrl(String url,Class<T> clazz) {
        try {
            url = dirHost+url ;
            String result = HttpURLConnectionUtil.doGet(url) ;
            BasicResponseBO<T> re = mapper.readValue(result, new TypeReference<BasicResponseBO<T>>() {});
            T body = mapper.readValue(mapper.writeValueAsString(re.getResult()), clazz);
            re.setResult(body);
            return re;
        } catch (Exception e) {
            return new BasicResponseBO<T>().setSuccess(false).setMessage("操作失败");
        }
    }

//    public static void main(String[] args) {
//        BasicResponseBO<List<VeBaseDeptBO>> list = new VeBaseManageServiceImpl().deptAll();
//        System.out.println(list+"--"+new VeBaseManageServiceImpl().dirHost);
//    }

}
